package com.zhiyume.controller;

import com.zhiyume.dao.entity.TPrize;
import com.zhiyume.page.PageReq;
import com.zhiyume.service.TPrizeService;
import com.zhiyume.service.common.ConstantEnum;
import com.zhiyume.service.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author wang
 * @version $Id: PrizeController.java, v 0.1 2019-01-24 17:55:03 wang Exp $$
 */
@Controller
@RequestMapping("/prize")
public class PrizeController {
    @Autowired
    private TPrizeService tPrizeService;

    /**
     * 根据Id查询信息
     * @return
     */
    @RequestMapping("/findById")
    @ResponseBody
    public Map findById(long id){
        TPrize prize = tPrizeService.findPrizeById(id);

        if(null == prize.getId()){
           return JsonUtil.returnResult(ConstantEnum.CONSTANT_MISS_PARA.getErrType(),ConstantEnum.CONSTANT_MISS_PARA.getErrMsg());
        }
        return JsonUtil.returnResult(ConstantEnum.CONSTANT_SUCCESS.getErrType(),ConstantEnum.CONSTANT_SUCCESS.getErrMsg(),prize);
    }

    /**
     * 添加信息
     * @param tPrize     * @return
     */
    @RequestMapping("/addPrize")
    @ResponseBody
    public Map addPrize(TPrize tPrize) {

        TPrize prize = new TPrize();
        
        String errType =ConstantEnum.CONSTANT_SUCCESS.getErrType();
        String errMsg =ConstantEnum.CONSTANT_SUCCESS.getErrMsg(); 
        try{
            prize = tPrizeService.addPrize(tPrize);
        }catch (Exception e){
            errType =ConstantEnum.CONSTANT_SYS_ERROR.getErrType();
            errMsg =e.getMessage();
        }

        return JsonUtil.returnResult(errType,errMsg,prize);

    }

    /**
     * page
     * @return
     */
    @RequestMapping("/prizeListPage")
    public ModelAndView prizeListPage(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("page/prize/prizeListPage");
        return modelAndView;
    }

    /**
     * 使用自定义mapper查询列表
     * @return
     */
    @RequestMapping("/getPrizeDataList")
    @ResponseBody
    public Map<String,Object> getPrizeDataList(TPrize reqVO,PageReq page){
        Map<String,Object> dataMap = new HashMap<String,Object>();
        prizeData(reqVO, page, dataMap);
        return dataMap;
    }

    /**
     * 列表过滤查询
     * @return
     */
    @RequestMapping("/searchPrizeList")
    @ResponseBody
    public Map<String,Object> searchPrizeList(TPrize reqVO, PageReq page){
        Map<String,Object> dataMap = new HashMap<String,Object>();
        prizeData(reqVO, page, dataMap);
        return dataMap;
    }
    
    private void prizeData(TPrize reqVO, PageReq page, Map<String, Object> dataMap) {
        List<TPrize> list = tPrizeService.queryPrizeList(reqVO,page);
        list = list.size() > 0 ? list : null;
        int count = tPrizeService.countPrize(reqVO);
        dataMap.put("list",list);
        dataMap.put("total",count);
        dataMap.put("page",page);
    }
    
    @RequestMapping("/addPrizePage")
    public ModelAndView addPrizePage(Model model){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("page/prize/prizeAddPage");
        model.addAttribute("prize",new TPrize());
        return modelAndView;
    }

    @RequestMapping("/editPrizePage")
    public ModelAndView editPersonPage(@RequestParam("id") long id, Model model){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("page/prize/prizeAddPage");
        TPrize tPrize = tPrizeService.findPrizeById(id);
        model.addAttribute("prize", tPrize);
        return modelAndView;
    }

    /**
     * 修改信息
     * @param tPrize     * @return
     */
    @RequestMapping("/updatePrize")
    @ResponseBody
    public Map updatePrize(@ModelAttribute("form") TPrize tPrize) {
        String errType =ConstantEnum.CONSTANT_SUCCESS.getErrType();
        String errMsg =ConstantEnum.CONSTANT_SUCCESS.getErrMsg();
        try {
             tPrizeService.updatePrize(tPrize);
        }catch (Exception e){
            errType =ConstantEnum.CONSTANT_SYS_ERROR.getErrType();
            errMsg =e.getMessage();
        }
        return JsonUtil.returnResult(errType,errMsg);
    }

    /**
     * 删除
     */
    @RequestMapping("/deletePrize")
    @ResponseBody
    public Map deletePrize(long id) {
        String errType =ConstantEnum.CONSTANT_SUCCESS.getErrType();
        String errMsg =ConstantEnum.CONSTANT_SUCCESS.getErrMsg();
        try {
            tPrizeService.deletePrize(id);
        }catch (Exception e){
            errType =ConstantEnum.CONSTANT_SYS_ERROR.getErrType();
            errMsg =e.getMessage();
        }
        return JsonUtil.returnResult(errType,errMsg);
    }
}